﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TOOLS;
using BLL;
namespace web.Controllers
{

    public class BaseController : Controller
    {
       
        public string ControllerName { get { return (string)Request.RequestContext.RouteData.Values["controller"]; } }
        public string ActionName
        {
            get{ return (string) Request.RequestContext.RouteData.Values["action"]; }
        }
        public ActionResult Index()
        {
            return View();
        }
        [AcceptVerbs(HttpVerbs.Get)]

        public FileContentResult VCode()
        {
            Random rand = new Random();

            var codeConfig = "100,40,5,50,16,1_|_2_|_3_|_4_|_5_|_6_|_7_|_8_|_9_|_0_|_A_|_B_|_C_|_D_|_E_|_F_|_G_|_H_|_i_|_J_|_K_|_L_|_M_|_N_|_X_|_P_|_Q_|_R_|_S_|_T_|_U_|_V_|_W_|_X_|_Y_|_Z_|_a_|_b_|_c_|_d_|_e_|_f_|_g_|_h_|_i_|_j_|_k_|_l_|_m_|_n_|_o_|_p_|_q_|_r_|_s_|_t_|_u_|_v_|_w_|_x_|_y_|_z,Verdana_|_Arial_|_MS Serif_|_Georgia_|_Times New Roman_|_Times_|_serif,4".Split(',');

            int width = int.Parse(codeConfig[0]);
            int height = int.Parse(codeConfig[1]);
            int defineLine = int.Parse(codeConfig[2]);
            int definePoint = int.Parse(codeConfig[3]);
            int defineFontSize = int.Parse(codeConfig[4]);
            string defineCode = codeConfig[5].Replace("_|_", ",");
            string defineFonts = codeConfig[6].Replace("_|_", ",");
            int defineCount = int.Parse(codeConfig[7]);

            int fontX = 0;
            int fontY = 0;

            width = width == 0 ? 100 : width;
            height = height == 0 ? 30 : height;

            if (defineFontSize != 0)
            {
                fontX = rand.Next(8, 15);
                fontY = rand.Next(2, 6);
            }

            defineFontSize = defineFontSize == 0 ? rand.Next(13, 20) : defineFontSize;

            string strVCode =TOOLS.Common.GetRandomString(defineCode, defineCount);

            string[] fonts = defineFonts.Split(',');

            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width, height);
            System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);
            System.Drawing.Color ct = System.Drawing.Color.FromArgb(255, System.Drawing.Color.Transparent);
            graphics.FillRectangle(new System.Drawing.SolidBrush(ct), new System.Drawing.Rectangle(0, 0, width, height));
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

            HttpContext.Session["vcode"] = strVCode;
#if DEBUG
            //System.IO.StreamWriter sw = new System.IO.StreamWriter(@"D:\Temp\TempVCode\" + strVCode);
            //sw.WriteLine(HttpContext.Session.SessionID);
            //sw.Close();
#endif
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            /*画背景*/
            try
            {
                //graphics.Clear(System.Drawing.Color.White);
                //graphics.Clear(System.Drawing.Color.Transparent);

                for (int count = 0; count < defineLine; count++)
                {
                    graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.FromArgb(rand.Next(1, 255), rand.Next(1, 255), rand.Next(1, 255))), rand.Next(bitmap.Width), rand.Next(bitmap.Height), rand.Next(bitmap.Width), rand.Next(bitmap.Height));
                }/*for结束*/

                /*字体和笔刷*/
                System.Drawing.Font font = new System.Drawing.Font(fonts[rand.Next(0, fonts.Length - 1)], defineFontSize, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic);
                System.Drawing.Brush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Color.FromArgb(rand.Next(1, 255), rand.Next(1, 255), rand.Next(1, 255)), System.Drawing.Color.FromArgb(rand.Next(1, 255), rand.Next(1, 255), rand.Next(1, 255)), 0, true);

                /*绘上文字*/
                graphics.DrawString(strVCode, font, brush, fontX, fontY);


                /*再在上边乱画点*/
                for (int count = 0; count < definePoint; count++)
                {
                    bitmap.SetPixel(rand.Next(bitmap.Width), rand.Next(bitmap.Height), System.Drawing.Color.FromArgb(rand.Next(0, 255)));
                }

                /*弄上一个边框*/
                //graphics.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red), 0, 0, bitmap.Width - 1, bitmap.Height - 1);

                //graphics.DrawPath(new System.Drawing.Pen(System.Drawing.Color.FromArgb(rand.Next(1, 255), rand.Next(1, 255), rand.Next(1, 255))), new System.Drawing.Drawing2D.GraphicsPath( System.Drawing.Drawing2D.FillMode.Winding));

                /*复制到内在中*/
                bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);

                /*显示图片*/
                //HttpContext.Current.Response.ContentType = "IMAGE/GIF";
                //bitmap.Save(Server.MapPath("\\") + "VCode.gif");
                //imgVCode.ImageUrl = "VCode.gif";
                //return File(ms.GetBuffer(), @"image/jpeg");

            }/*try*/
            catch (Exception error_Paint)
            {
                object obj = error_Paint;
            }/*catch*/
            finally
            {
                graphics.Dispose();
                bitmap.Dispose();
            }/*finally*/

            return File(ms.GetBuffer(), @"image/gif");
        }
    }
}
